package com.englisheveryday.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.englisheveryday.R;
import com.englisheveryday.constants.Constants;
import com.englisheveryday.service.SystemService;
import com.englisheveryday.utils.ActivityManager;
import com.englisheveryday.utils.L;

/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-10-14 上午9:55:12
 * @version 1.1
 */

public class SettingActivity extends Activity {

	private LinearLayout audioDownload,feedback,checkupdate;
	private Handler handler;
	private SystemService systemService;
	private String url;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_activity);
		ActivityManager.add(this);
		setViews();
		setListeners();
		handler = new SettingHandler();
		systemService = new SystemService(this);
	}
	
	public void setViews(){
		audioDownload = (LinearLayout)findViewById(R.id.audiodownload);
		feedback = (LinearLayout)findViewById(R.id.feedbacklayout);
		checkupdate = (LinearLayout)findViewById(R.id.checkupdatelayout);
	}
	
	public void setListeners(){
		audioDownload.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(SettingActivity.this, AudioDownloadActivity.class);
				startActivity(intent);
			}
		});
		
		feedback.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(SettingActivity.this, FeedBackActivity.class);
				startActivity(intent);
			}
		});
	
		checkupdate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(SettingActivity.this, R.string.checkupdate_checking, Toast.LENGTH_SHORT).show();
				new Thread(new CheckUpdateThread()).start();
			}
		});
	}
	
	class SettingHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case Constants.SYSTEM_CHECKUPDATE_HASUPDATE : {
				AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
				builder.setTitle(R.string.update_title);
				builder.setMessage(R.string.update_message);
				builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(url==null||"".equals(url)||!url.startsWith("http://"))
							return;
						Intent intent = new Intent("android.intent.action.VIEW",Uri.parse(url));
						startActivity(intent);
					}
				});
				builder.setNegativeButton(R.string.cancle, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						
					}
				});
				builder.show();
			}
			break;
			case Constants.SYSTEM_CHECKUPDATE_NOUPDATE : {
				Toast.makeText(SettingActivity.this, R.string.checkupdate_noupdate, Toast.LENGTH_SHORT).show();
			}
			break;
			}
			super.handleMessage(msg);
		}
		
	}
	
	//检查更新
	class CheckUpdateThread implements Runnable {
		@Override
		public void run() {
			//得到当前版本号
			try{
				PackageManager pm = SettingActivity.this.getPackageManager();  
		        PackageInfo pi = pm.getPackageInfo(SettingActivity.this.getPackageName(), 0);  
				url = systemService.checkupdate(Double.parseDouble(pi.versionName));
				L.d(url);
				if(url==null||"".equals(url))
					handler.sendEmptyMessage(Constants.SYSTEM_CHECKUPDATE_NOUPDATE);
				else
					handler.sendEmptyMessage(Constants.SYSTEM_CHECKUPDATE_HASUPDATE);
			}catch(Exception e){
				e.printStackTrace();
				handler.sendEmptyMessage(Constants.SYSTEM_CHECKUPDATE_NOUPDATE);
			}
			
		}
	}
}

